package net.xju.dao;

import net.xju.common.utils.JdbcUtilsV1;
import net.xju.common.utils.PageInfo;
import net.xju.entity.Announcement;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class AnnouncementDao {
    public List<Announcement> list() {
        Connection connection = JdbcUtilsV1.getConnection();
        String sql = "select * from announcement";
        List<Announcement> announcementList = JdbcUtilsV1.executeQuery(connection, Announcement.class, sql);
        return announcementList;
    }

    public Announcement selectById(int id) {
        String sql = "SELECT id,title,content,create_time createTime,update_time updateTime FROM announcement WHERE deleted =0 AND id = ?";
        return JdbcUtilsV1.executeQueryOne(JdbcUtilsV1.getConnection(), Announcement.class, sql, id);
    }

    // 在 AnnouncementDao.java 中
    public void insert(Announcement announcement) {
        String sql = "INSERT INTO announcement(title, content, publish_date, create_time, update_time) VALUES(?, ?, ?, ?, ?)";
        int i = JdbcUtilsV1.executeUpdate(sql,
                announcement.getTitle(),
                announcement.getContent(),
                announcement.getPublishDate(), // 确保这个值不为null
                announcement.getCreateTime(),
                announcement.getUpdateTime());
        System.out.println(i);
    }

    public Boolean update(Announcement announcement) {
        StringBuilder sql = new StringBuilder("UPDATE announcement SET ");
        List<Object> params = new ArrayList<>();
        if (announcement.getTitle() != null && !announcement.getTitle().isEmpty()) {
            sql.append(" title =?, ");
            params.add(announcement.getTitle());
        }
        if (announcement.getContent() != null && !announcement.getContent().isEmpty()) {
            sql.append(" content =?, ");
            params.add(announcement.getContent());
        }
        if (Objects.nonNull(announcement.getUpdateTime())) {
            sql.append(" update_time =?, ");
            params.add(announcement.getUpdateTime());
        }
        if (params.size() > 0) {
            sql.deleteCharAt(sql.lastIndexOf(","));
        }
        sql.append(" WHERE id = ?");
        params.add(announcement.getId());
        int i = JdbcUtilsV1.executeUpdate(sql.toString(), params.toArray());
        return i > 0;
    }

    public boolean deleteById(int id) {
        String sql = "update announcement set deleted = 1 where id = ?";
        int i = JdbcUtilsV1.executeUpdate(sql, id);
        return i > 0;
    }

    public PageInfo<Announcement> pageList(String title, Integer pageNo, Integer pageSize) {
        String sql = "SELECT id,title,content,create_time createTime,update_time updateTime FROM announcement WHERE deleted =0 ";
        List<Object> params = new ArrayList<>();
        if (title != null && !title.isEmpty()) {
            sql += " and title like ?";
            params.add("%" + title + "%");
        }
        sql += "limit ?,?";
        params.add((pageNo - 1) * pageSize);
        params.add(pageSize);
        List<Announcement> announcements = JdbcUtilsV1.executeQuery(Announcement.class, sql, params.toArray());
        String countSql = "select count(*) from announcement where deleted =0 ";
        long count = JdbcUtilsV1.executeQueryOneValue(countSql);
        PageInfo<Announcement> pageInfo = new PageInfo<>(pageNo, pageSize, (int) count, announcements);
        return pageInfo;
    }
}
